<?php

class DB
{
    //属性
    protected $link;
    protected $redis;

    /**
     * 连接数据库
     * DB constructor.
     */
    public function __construct()
    {
        $this->link = new PDO('mysql:host=127.0.0.1;dbname=1118', 'root', 'root');
        $this->redis = new Redis();
        $this->redis->connect('127.0.0.1','6379');
    }

    /**
     * 添加数据
     * @param $tableName
     * @param $data
     * @return false|string
     */
    public function getAdd($tableName,$data)
    {
        $sql = "insert into $tableName ";
        //判断是否为空
        if (!empty($data)){
            $k = '';
            $v = '';
            foreach($data as $key => $value){
                $k .= " $key ,";
                $v .= " '$value' ,";
            }
        }
        //切割最后的逗号
        $k = substr($k,0,-1);
        $v = substr($v,0,-1);
        $sql .= " ($k) value ($v) ";
        $res = $this->link->exec($sql);
        //判断是否添加成功
        if($res){
            $arr['code'] = 0;
            $arr['msg'] = '添加成功';
            $arr['data'] = [];
        }else{
            $arr['code'] = 1;
            $arr['msg'] = '添加失败';
            $arr['data'] = [];
        }
        //返回值
        return json_encode($arr);
    }

    /**
     * 展示搜索分页
     * @param $tableName
     * @param $data
     * @param $page
     * @param $size
     * @return false|string
     */
    public function getShow($tableName,$data,$page,$size)
    {
        $sql1 = "select * from $tableName ";
        $stmtname = $this->link->query($sql1);
        $total = $stmtname->rowCount();
        $limit = ($page-1)*$size;

        $sql = "select * from $tableName ";
        $limi = "limit $limit,$size";
        //判断是否为空
        if (!empty($data)){
            $query_redis = $this->redis->get($data['name']);
            if($query_redis){
                return json_encode($query_redis);
            }
            $where = ' where ';
            foreach($data as $key => $value){
                $where .= "$key".'='." '$value' ";
            }
            $sql .= $where.$limi;
        }else{
            $sql .= $limi;
        }
//        echo $sql;die;
        //执行sql语句
        $stmt = $this->link->query($sql);
        $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
        if(!empty($res)){
           $this->redis->set(json_encode($res),true);
        }
        //判断是否添加成功
        if($res){
            $arr['code'] = 0;
            $arr['msg'] = '成功';
            $arr['data'] = $res;
            $arr['total'] = $total;
        }else{
            $arr['code'] = 1;
            $arr['msg'] = '失败';
            $arr['data'] = [];
        }
        //返回值
        return json_encode($arr);
    }

    /**
     * 删除
     * @param $tableName
     * @param $data
     * @return false|string
     */
    public function getDel($tableName,$data)
    {
        $sql = "delete from $tableName ";
        //判断是否为空
        if (!empty($data)){
            $where = ' where ';
            foreach($data as $key => $value){
                $where .= "$key".'='."'$value'";
            }
            $sql .= $where;
        }
        //执行sql语句
        $res = $this->link->exec($sql);
        //判断是否添加成功
        if($res){
            $arr['code'] = 0;
            $arr['msg'] = '删除成功';
            $arr['data'] = [];
        }else{
            $arr['code'] = 1;
            $arr['msg'] = '删除失败';
            $arr['data'] = [];
        }
        //返回值
        return json_encode($arr);
    }

}
























